<template>
  <div class="app-container">

    <div class="" style="background: #ffffff; padding: 20px">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="捐赠人" prop="donateName">
          <el-input v-model="queryParams.donateName" placeholder="请输入捐赠人" clearable
                    @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="联系方式" prop="telPhone">
          <el-input v-model="queryParams.telPhone" placeholder="请输入联系方式" clearable
                    @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="捐赠来源" prop="sourceCategory" label-width="120px">
          <el-select v-model="queryParams.sourceCategory" placeholder="请选择捐赠来源">
            <el-option v-for="item in source_options" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                     v-hasPermi="['donate:source:add']"
          >新增
          </el-button>
        </el-col>
        <!-- <el-col :span="1.5">
          <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
            v-hasPermi="['donate:source:edit']">修改
          </el-button>
        </el-col> -->
        <el-col :span="1.5">
          <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                     v-hasPermi="['donate:source:remove']"
          >删除
          </el-button>
        </el-col>
        <!-- <el-col :span="1.5">
          <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
            v-hasPermi="['donate:source:export']">导出
          </el-button>
        </el-col> -->
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
    </div>

    <div class="" style="background: #ffffff; padding: 20px; margin: 20px 0 0">
      <el-table v-loading="loading" :data="sourceList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column type="index" width="50"/>
        <el-table-column label="捐赠人" align="center" prop="donateName"/>
        <el-table-column label="联系方式" align="center" prop="telPhone"/>
        <el-table-column label="是否匿名" align="center" prop="anonymous">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.anonymous" active-color="#13ce66" inactive-color="#ff4949" active-value="0"
                       inactive-value="1" @change="changeStatus(scope.row)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="捐赠来源" align="center" prop="sourceCategory">
          <template slot-scope="scope">
            <span v-if="scope.row.sourceCategory==1">个人</span>
            <span v-if="scope.row.sourceCategory==2">公司</span>
            <span v-if="scope.row.sourceCategory==3">组织</span>
            <span v-if="scope.row.sourceCategory==4">政府</span>
            <!--          <span>{{ changeSource(scope.row.sourceCategory) }}</span>-->
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                       v-hasPermi="['donate:source:edit']"
            >修改
            </el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row, scope.$index)"
                       v-hasPermi="['donate:source:remove']"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                  @pagination="getList"
      />
    </div>

    <!-- 添加或修改捐赠来源对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="捐赠人" prop="donateName">
          <el-input v-model="form.donateName" placeholder="请输入捐赠人"/>
        </el-form-item>
        <el-form-item label="联系方式" prop="telPhone">
          <el-input v-model="form.telPhone" placeholder="请输入联系方式"/>
        </el-form-item>
        <el-form-item label="是否匿名" prop="anonymous">
          <el-switch v-model="form.anonymous" active-color="#13ce66" inactive-color="#ff4949" active-value="0"
                     inactive-value="1"
          ></el-switch>
        </el-form-item>
        <el-form-item label="捐赠来源" prop="sourceCategory">
          <el-select v-model="form.sourceCategory" placeholder="请选择捐赠来源">
            <el-option v-for="item in form_options" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSource, getSource, delSource, addSource, updateSource } from '@/api/donate/source'

export default {
  name: 'Source',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 捐赠来源表格数据
      sourceList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        donateName: null,
        telPhone: null,
        sourceCategory: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        donateName: [
          { required: true, message: '请输入捐赠人', trigger: 'change' }
        ],
        // telPhone: [
        //   { required: true, message: '请输入联系方式', trigger: 'change' }
        // ],
        sourceCategory: [
          { required: true, message: '请选择捐赠来源', trigger: 'change' }
        ],
        phone: [
          { required: true, message: '请输入联系方式', trigger: 'change' }
        ]
      },
      //捐赠来源  个人，2：公司，3：组织，4：政府
      source_options: [{
        value: null,
        label: '全部'
      }, {
        value: 1,
        label: '个人'
      }, {
        value: 2,
        label: '公司'
      }, {
        value: 3,
        label: '组织'
      }, {
        value: 4,
        label: '政府'
      }],
      //表单捐赠来源
      form_options: [{
        value: '1',
        label: '个人'
      }, {
        value: '2',
        label: '公司'
      }, {
        value: '3',
        label: '组织'
      }, {
        value: '4',
        label: '政府'
      }]
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询捐赠来源列表 */
    getList() {
      this.loading = true
      listSource(this.queryParams).then(response => {
        this.sourceList = response.pageData
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        donateName: null,
        telPhone: null,
        anonymous: '0',
        sourceCategory: '1',
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      console.log(this.queryParams)
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加捐赠来源'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getSource(id).then(response => {
        this.form = response
        // this.form.sourceCategory = parseInt(this.form.sourceCategory)
        this.open = true
        this.title = '修改捐赠来源'
      })
    },
    // 匿名状态修改
    changeStatus(row) {
      console.log(row)
      this.form.anonymous = row.anonymous
      this.form.id = row.id
      updateSource(this.form).then(response => {
        this.$modal.msgSuccess('修改成功')
        this.getList()
      })
    },
    // 捐赠来源转化
    // changeSource(type) {
    //   if (type == '1') {
    //     return '个人'
    //   } else if (type == '2') {
    //     return '公司'
    //   } else if (type == '3') {
    //     return '组织'
    //   } else if (type == '4') {
    //     return '政府'
    //   }
    // },
    changtype(type) {
      console.log(typeof (type))
      // this.queryParams.sourceCategory = type
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSource(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addSource(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row, id) {
      const ids = (id + 1) || this.ids
      const delIds = row.id || this.ids
      this.$modal.confirm('是否确认删除该捐赠来源？').then(function() {
        return delSource(delIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('donate/source/export', {
        ...this.queryParams
      }, `source_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
